from django import forms
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError

from app01 import models
from app01.utils.bootstrap import BootStrapModelForm,BootStrapForm
from app01.utils.encrypt import md5

class UserModelForm(BootStrapModelForm):
    class Meta:
      model = models.UserInfo
      fields = ['name','password','age','create_time','gender','depart']

class PrettyModelForm(BootStrapModelForm):
    # mobile = forms.CharField(
    #     label="手机号",
    #     validators=[RegexValidator(r'^1[3-9]\d{9}$','手机号格式错误')]
    # )
    class Meta:
      model = models.PrettyNum
      fields = "__all__"
    #   fields = ['mobile','price','level','status']      
    #   exclude = ['level']
    
    def clean_mobile(self):
        txt_mobile = self.cleaned_data['mobile']
        if len(txt_mobile) != 11 :
            raise ValidationError('格式错误')
        exists = models.PrettyNum.objects.filter(mobile=txt_mobile).exists()
        if exists:
            raise ValidationError('手机号已存在')
        return txt_mobile

class PrettyEditModelForm(BootStrapModelForm):    
    class Meta:
      model = models.PrettyNum     
      exclude = ['mobile']
    # def __init__(self, *args,**kwargs):
    #     super().__init__(*args,**kwargs)
    #     for name,field in self.fields.items():
    #         # if name == "password":
    #         #     continue
    #         field.widget.attrs = {"class" : "form-control","placeholder":field.label}
    def clean_mobile(self):
        # 当前编辑的哪一行的ID
        # self.instance.pk

        txt_mobile = self.cleaned_data['mobile']
        if len(txt_mobile) != 11 :
            raise ValidationError('格式错误')
        exists = models.PrettyNum.objects.exclude(id=self.instance.pk).filter(mobile=txt_mobile).exists()
        if exists:
            raise ValidationError('手机号已存在')
        return txt_mobile

class AdminModelForm(BootStrapModelForm):
    confirm_password = forms.CharField(label="确认密码",widget=forms.PasswordInput)
    class Meta:
        model = models.Admin
        fields = ["username" , "password","confirm_password"]
        widgets = {
            "password" : forms.PasswordInput(render_value=True)
        }
    def clean_password(self):
        pwd = self.cleaned_data.get("password")
        return md5(pwd)
    def clean_confirm_password(self):
        pwd = self.cleaned_data.get("password")
        confirm = md5(self.cleaned_data.get("confirm_password"))
        if pwd != confirm:
            raise ValidationError("密码不一致")
        return confirm

class AdminEditModelForm(BootStrapModelForm):
    class Meta:
        model = models.Admin
        fields = ["username"]

class AdminResetModelForm(BootStrapModelForm):
    confirm_password = forms.CharField(label="确认密码",widget=forms.PasswordInput)
    class Meta:
        model = models.Admin
        fields = ["password","confirm_password"]
        widgets = {
            "password" : forms.PasswordInput(render_value=True)
        }
    def clean_password(self):
        pwd = self.cleaned_data.get("password")
        md5_pwd = md5(pwd)
        exists = models.Admin.objects.filter(id = self.instance.pk,password=md5_pwd).exists()
        if exists:
            raise ValidationError("密码不能与之前一致")
        return md5_pwd
    def clean_confirm_password(self):
        pwd = self.cleaned_data.get("password")
        confirm = md5(self.cleaned_data.get("confirm_password"))
        if pwd != confirm:
            raise ValidationError("密码不一致")
        return confirm

class LoginModelForm(forms.ModelForm):
     class Meta:
        model = models.Admin
        fields = ["username","password"]
 
class LoginForm(BootStrapForm):
    username = forms.CharField(
        label="用户名",
        widget=forms.TextInput,
        required=True
    )
    password = forms.CharField(
        label="密码",
        widget=forms.PasswordInput(render_value=True),
        required=True
    )

    code = forms.CharField(
        label="验证码",
        widget=forms.TextInput,
        required=True
    )

    def clean_password(self):
        pwd = self.cleaned_data.get("password")
        return md5(pwd)
    
class TaskModelForm(BootStrapModelForm):
    class Meta:
        model = models.Task
        fields = "__all__"
        widgets = {
            # "detail": forms.Textarea,
            "detail": forms.TextInput
        }

class OrderModelForm(BootStrapModelForm):
    class Meta:
        model = models.Order
        # fields = "__all__"
        # fields = [""]
        exclude = ["oid", 'admin']